package com.gxdf.extendedcontrols.gxdfclient.components;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



import com.gxdf.extendedcontrols.gxdfclient.data.Field;
import com.gxdf.extendedcontrols.gxdfclient.data.ValidationMessage;
import com.gxdf.extendedcontrols.gxdfclient.data.ValidationResult;
import com.gxdf.extendedcontrols.gxdfclient.data.ValidationType;

import android.content.Context;
import com.google.gson.stream.JsonWriter;
import android.widget.LinearLayout;

public abstract class GxdfField extends LinearLayout implements IGxdfItem {	
	
	Field field;	
	List<String> actualValues;
	List<ValidationResult> validRules;
	Pattern pattern;
	Matcher matcher;
	boolean isValid;
	
	public GxdfField(Context context, Field field) {
		super(context);
		setPadding(10, 2, 10, 2);
		isValid = true;
		this.field = field;
		actualValues= new ArrayList<String>();
		validRules = new ArrayList<ValidationResult>();
	}	
	
	public Field getField() {
		return field;
	}

	public void setField(Field field) {
		this.field = field;
	}

	public String getFieldName(){
		return field.getName();
	}
	
	public boolean errorRequired(){		
		boolean error = actualValues.size()==0 && field.isRequired();
		if (!error && field.isRequired()){
			for(int i=0;i<actualValues.size();i++){
				if(actualValues.get(i).isEmpty())
					error = true;
			}
		}
		return error;
	}
	
	public List<ValidationResult> getValidationList(){
		return validRules;
	}
	
	public void runValidationRules(){
		
		validRules.clear();
		if(errorRequired()){
			isValid=false;
			ValidationResult vr = new ValidationResult(field.getName(),ValidationType.REQUIRED, ValidationMessage.getMessage(ValidationType.REQUIRED));
			validRules.add(vr);
		}
		
		if(errorFormat()){
			isValid=false;
			ValidationResult vr = new ValidationResult(field.getName(),ValidationType.FORMAT, ValidationMessage.getMessage(ValidationType.FORMAT));
			validRules.add(vr);			
		}
		
		if(!(errorRequired()) && !(errorFormat()))
			validRules.clear();
	}
	
	
	
	public void buildjson(JsonWriter writer) throws IOException{
		
		writer.beginObject();
		writer.name("Name").value(field.getName());
		writer.name("Type");
		
		writer.beginObject();
		
		field.getType().buildJson(writer);
		
		writer.endObject();
		
		
		writer.name("Label").value(field.getLabelValue());
		writer.name("Length").value(field.getLength());
		writer.name("FormatValidation").value(field.getFormatValidation());
		writer.name("Required").value((field.isRequired() ? "Y" : "N"));
		
		writer.name("Values");
		writer.beginObject();
		writer.name("Value");
		writer.beginArray();
	
		for(int i=0;i<this.getActualValues().size();i++){

			writer.beginObject();
			writer.name("Value").value(this.getActualValues().get(i));
			writer.name("ValueNumber").value(String.valueOf(i+1));	
			writer.endObject();
		}
		
		writer.endArray();
		writer.endObject();
		writer.name("IsCollection").value((field.isCollection() ? "Y" : "N"));
		writer.name("Concept");
		writer.beginObject();
		writer.name("ConceptName").value(field.getConcepto());
		writer.name("IsCollection").value((field.isCollection() ? "Y" : "N"));
		writer.endObject();
		writer.endObject();
		
	}

	
	
	protected abstract boolean errorFormat();
	protected abstract boolean errorLength();	
	
	
}
